import React, { useState, useEffect, useMemo } from 'react';
import { get_fix, get_ck, load_col, load_etype } from './cmd';
import { KFile, loc, ColView, HandView, obj_str, no_del, is_empty, PanelFind, filter_row, MCmd, useCmd, useHigh, Btn, GridTable } from 'sui';
import { load_station } from 'tech/cmd';
import { useAccess } from 'cache';

const AccountView = () => {
    const [station, setStation] = useState();
    const [etype, setEType] = useState();
    const [col, setCol] = useState();
    const [high] = useHigh(140, 100);
    const [no_acc] = useAccess();
    const umd = useCmd("equip/list", null, {count: true});

    useEffect(() => {
        load_etype(setEType);
        load_col(setCol);
        load_station(setStation);
    }, []);

    const getRow = useMemo(() => {
        const lst = ["sn", "name", "tp", "wsn"];
        return filter_row(umd.value, lst, umd.filter);
    }, [umd.value, umd.filter]);

    const hImport = (val) => {
        if (is_empty(col)) {
            alert(loc("l_686"));
            return;
        }

        if (is_empty(val)) {
            alert(loc("l_276"));
            return;
        }

        umd.update("equip/madd", {v: val});
    };

    const hCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const ret = [];
        col.forEach(d => {
            if (d.sn === "tp") {
                d.ctl = etype;
            } else if (d.sn === "wsn") {
                d.ctl = station;
            }

            ret.push(d);
        });

        return ret;
    };

    const act = {
        "add": (v, h) => umd.update("equip/add", {"v": v}, h),
        "edit": (v, h) => umd.update("equip/md", {"v": [v]}, h),
    };

    if (umd.isFin("edit") || umd.isFin("add")) {
        return <HandView umd={umd} title={loc("l_872")} act={act} col={hCol()} ck={get_ck(umd.cur, umd.value, col)}/>;
    }

    if (umd.isFin("col")) {
        const list = [
            {sn: "equip", name: loc("l_145"), fix: get_fix()},
        ];

        const hreturn = () => {
            load_col(setCol);
            umd.hreturn();
        };

        return <ColView hreturn={hreturn} title={loc("l_872")} subtitle={loc("l_221")} list={list} cur="equip"/>;
    }

    const hDel = (val) => {
        if (no_del()) {
            return;
        }

        umd.update("equip/rm", {"k": obj_str(val, "sn")});
    };

    const getCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const tmp = [
            {name: loc("l_193"), key: 'index', width: 70, frozen: true},
            {name: loc("l_238"), key: 'e_opt', width: 120, frozen: true, formatter: (props) => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <MCmd hcmd={() => umd.pact(null, d, "edit")} itype="edit" disabled={no_acc("kg")}/>
                        <MCmd hcmd={() => hDel(d)} itype="del" disabled={no_acc("kg")}/>
                    </div>
                );
            }},
        ];

        col.forEach(d => {
            if (d.sn === "tp") {
                tmp.push({name: d.name, key: d.sn, width: 200, formatter: (props) => {
                    const d = props.row;
                    if (is_empty(etype)) {
                        return null;
                    }

                    const f = etype.find(t => t.sn === d.tp);
                    if (!f) {
                        return null;
                    }

                    return obj_str(f, "name");
                }});
            } else {
                tmp.push({name: d.name, key: d.sn, width: 200});
            }

        });

        return tmp;
    };

    const getList = () => {
        if (is_empty(col)) {
            return null;
        }

        return col.map(d => d.sn);
    };

    const rowTempl = () => {
        const tmp = getList();
        if (is_empty(tmp)) {
            return null;
        }

        const v = {};
        tmp.forEach(k => v[k] = "1");

        return [v];
    };

    const Right = () => (
        <div className="inline-flex">
            <Btn className="mr-4" hcmd={() => umd.pact(null, null, "col")} disabled={no_acc("kg")}>{loc("l_221")}</Btn>
            <KFile umd={umd} title={loc("l_872")} col={getList()} row={rowTempl()} hcmd={hImport} no_acc_in={no_acc("kg")}/>
            <Btn color="green" hcmd={() => umd.pact(null, null, "add")} className="ml-4" disabled={no_acc("kg")}>{loc("l_239")}</Btn>
        </div>
    );

    return (
        <div className="flex flex-col">
            <PanelFind left={loc("l_872")} right={<Right/>} hcmd={umd.setFilter}/>
            <GridTable high={high} col={getCol()} row={getRow} rowHeight={42}/>
        </div>
    );
};

export {AccountView};
